<template>
  <Table :columns="columns" :tabledata="tabledata" :loading="loading" :pagination="pagination" typeEnum='miaoModel' @onClickPagination="onClickPagination" @onClickupdate='onClickupdate' @onClickDelete='onClickDelete' />
</template>

<script lang='ts'>
import { reactive, toRefs } from "vue";
import Table from "@/components/Table/Table.vue";
import columns from "@/views/MiaoList/method/TableConfig";
import createStore from "@/store/index";
import {
  TableData,
  TypeEnum,
} from "@/CommonAssets";
export default {
  name: " ",
  components: {
    Table,
  },
  props: {
    RecordData: Object,
  },
  setup(props: any) {
    /* 页面交互数据 */
    const data = reactive({
      loading: false,
      tabledata: [],
      pagination: {
        total: 0,
        pageSize: 6,
        hideOnSinglePage: true,
        current: 1,
      },
    } as any);

    async function tabledata(pageNo = 1) {
      data.loading = true;
      const { tabledata, total } = await TableData(
        TypeEnum.miaoModel,
        pageNo,
        createStore.state.userDataList._id
      );
      data.loading = false;
      data.tabledata = tabledata;
      data.pagination.total = total;
    }
    tabledata();

    /* 分页事件 */
    const onClickPagination = async (current: number) => {
      data.pagination.current = current;
      tabledata(current);
    };

    /* 更新数据 */
    interface updateData {
      index: number;
      accountPool?: string;
      merge?: string | null;
      mergeType?: string | null;
      codestatus?: boolean;
    }

    function onClickupdate(e: updateData) {
      if (e.accountPool) {
        data.tabledata[e.index].accountPool = e.accountPool;
      }

      if (e.merge && e.mergeType) {
        data.tabledata[e.index].mergeType = e.mergeType;
        data.tabledata[e.index].merge = e.merge;
      } else {
        data.tabledata[e.index].mergeType = null;
        data.tabledata[e.index].merge = null;
      }

      if (e.codestatus) {
        data.tabledata[e.index].codestatus = true;
      } else {
        data.tabledata[e.index].codestatus = false;
      }
    }

    /* 删除应用 */
    function onClickDelete(index: number) {
      // data.tabledata.splice(index, 1);
      tabledata(data.pagination.current);
    }

    return {
      ...toRefs(data),
      onClickPagination,
      onClickupdate,
      onClickDelete,
      columns,
    };
  },
};
</script>